package com.x.reader.controller;

import com.x.framework.Base;
import com.x.framework.exception.BusinessException;
import com.x.framework.redis.XRedisTemplate;
import com.x.reader.model.User;
import com.x.reader.service.UserService;
import com.x.reader.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;
import java.util.*;

@Controller
@RestController
public class WxController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    private static ModelAndView loginView = new ModelAndView("login.jsp");

    @Autowired
    private UserService userService;

    @Autowired
    private XRedisTemplate xRedisTemplate;

    public WxController() {

    }

    @RequestMapping(
            value = {"/wx/authorize"}
    )
    public void authorize(HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("******************WX_AUTHORIZE_URL=" + Constant.WX_AUTHORIZE_URL);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
//        String redirectUri = URLEncoder.encode(Constant.WX_REDIRECT_URL, "UTF-8");
        response.sendRedirect(Constant.WX_AUTHORIZE_URL);
    }

    @RequestMapping(
            value = {"/wx/pay/notify"}
    )
    public ModelAndView payNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map map = request.getParameterMap();
            return loginView;
        } catch (Exception e) {
            throw new BusinessException("wx get token is wrong", e);
        }
    }

}
